package com.jy.rhin.support.parsing;

import com.alibaba.fastjson.JSONArray;
import com.jy.rhin.application.enums.DocumentTypeEnum;
import com.jy.rhin.facade.dto.DocumentModule;
import com.jy.rhin.facade.dto.TemplateRespDto;
import com.jy.rhin.support.parsing.enums.DeathElementEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 死亡医学证明
 * @Author: lww
 * @Date: 2022/6/21 9:10
 */
@Component
public class SiWangYXZMParser extends BaseDocumentParser {

    @Override
    public DocumentTypeEnum getDocumentType() {
        return DocumentTypeEnum.MEDICAL_CERTIFICATE_OF_DEATH;
    }

    /**
     * 死亡医学证明文档解析
     *
     * @param json                xmlJSON
     * @param templateRespList    查询的返回参数
     */
    @Override
    public List<TemplateRespDto> documentParsing(Object json, List<TemplateRespDto> templateRespList) {
        //文档头部分
        List<TemplateRespDto> headList = new ArrayList<>();
        LinkedHashMap<DeathElementEnum, List<TemplateRespDto>> deathListMap = new LinkedHashMap<>();
        String arrayType = null;
        //补充对应文档部分字段List
        int number = 1;
        for (TemplateRespDto templateRespDto : templateRespList) {
            String element = templateRespDto.getYuanSuMC();
            String type = templateRespDto.getType();
            //list节点遍历完成时重新赋值
            if ((!StringUtils.isEmpty(arrayType)) && compareDeathNum(element, arrayType, type)) {
                arrayType = null;
            }
            if (!StringUtils.isEmpty(type) || Objects.nonNull(DeathElementEnum.lookup(arrayType))) {
                DeathElementEnum[] values = DeathElementEnum.values();
                for (DeathElementEnum deathElementEnum : values) {
                    if (Objects.equals(deathElementEnum.getCode(), type) || Objects.equals(deathElementEnum.getName(), arrayType)) {
                        arrayType = deathElementEnum.getName();
                        if (deathListMap.containsKey(deathElementEnum)) {
                            List<TemplateRespDto> templateRespDtoListMap = deathListMap.get(deathElementEnum);
                            templateRespDtoListMap.add(templateRespDto);
                        } else {
                            List<TemplateRespDto> templateRespDtoListMap = new ArrayList<>();
                            templateRespDtoListMap.add(templateRespDto);
                            deathListMap.put(deathElementEnum, templateRespDtoListMap);
                        }
                        break;
                    }
                }
            } else {
                headList.add(templateRespDto);
            }
            templateRespDto.setNumber(number);
            number++;
        }
        //获取value值
        pathValue(headList, json, null);
        List<TemplateRespDto> templateRespDtoList = new ArrayList<>(headList);
        JSONArray headIdArray = objectPathArray(HEAD_ID_KEY, json);
        JSONArray componentArray = objectPathArray(COMPONENT_KEY, json);
        JSONArray entryArray = objectPathArray(ENTRY_KEY, json);
        for (Map.Entry<DeathElementEnum, List<TemplateRespDto>> entry : deathListMap.entrySet()) {
            DeathElementEnum key = entry.getKey();
            List<TemplateRespDto> valueList = entry.getValue();
            if (Objects.equals(HEAD, key.getType())) {
                Object object = arrayPathObject(HEAD_ID_CODE, key.getCode(), headIdArray);
                pathValue(valueList, object, 3);
            } else if (Objects.equals(COMPONENT, key.getType())) {
                Object object = arrayPathObject(SECTION_CODE, key.getCode(), componentArray);
                pathValue(valueList, object, 3);
            } else {
                Object object = arrayPathObject(OBSERVATION_CODE, key.getCode(), entryArray);
                pathValue(valueList, object, 5);
            }
            templateRespDtoList.addAll(valueList);
        }
        return templateRespDtoList.stream().sorted(Comparator.comparingInt(TemplateRespDto::getNumber)).collect(Collectors.toList());
    }

    @Override
    public List<DocumentModule> createBodyModule() {
        List<DocumentModule> list = new ArrayList<>();
        //文档体模块
        list.add(new DocumentModule("死亡信息章节","/component/structuredBody/component/section/code[@displayName='死亡信息']"));
        list.add(new DocumentModule("诊断记录章节","/component/structuredBody/component/section/code[@code='29548-5']"));
        return list;
    }


    /**
     * 校验是否需要重新赋值arrayType
     *
     * @param element
     * @param arrayType
     * @param type
     * @return
     */
    private boolean compareDeathNum(String element, String arrayType, String type) {
        return !StringUtils.isEmpty(type) || containsNumber(element) <= Integer.parseInt(arrayType.substring(arrayType.indexOf(SPOT) + 1));
    }
}
